import {mapGetters} from 'vuex'
import {
	getOrder,
	guestsList,
	ableCoupon,
	getOrderPrice,
	getActivites,
	orderSave,
	checkPayStatus,
	preSecondOrder,
	clacSecondPrice
	} from '@/api/api.js'
export default{
	data() {
		return {
			num:null,
			minDate:this.$u.timeFormat(new Date().getTime()-1000*60*60*6),
			show:false,
			peopleNum:1,
			radio:'1',
			payTypeArray:['微信支付','科爱豆支付'],
			payTypeIndex:0,
			modalName:null,
			occupants:[],
			occupantsIndex:[],
			orderNo:null,
			hotelObj:{
				deposit:0
			},
			couponNum:0,
			orderMoney:0,
			couponPrice:0,
			activitesPrice:0,
			couponList:[],
			activitesList:[],
			activites:{},
			coupon:{},
			switchA:false,
			hours:1,
			secondPrice:0,
			roomTypeImage:[]
		};
	},
	computed:{
		...mapGetters(['time','type','chooseCity']),
	},
	onLoad(e) {
		this.hotelObj = e;
		this.roomTypeImage = uni.roomTypeImage;
		getOrder().then(res=>{
			this.orderNo=res.data;
		})
		this.getActivites();
		this.initTime();
		this.getGuests();
		this.getAbleCoupon();
		this.getOrderPrice();
		uni.$on("selectCoupon",(coupon)=>{
			this.coupon = coupon;
			this.changeMoney();
		});
		uni.$on('selectActivites',(activites)=>{
			this.activites = activites;
			this.changeMoney();
		})
		uni.$on('addPeople',()=>{
			this.getGuests();
		})
		if(this.type){
			this.sliderChange();
		}
	},
	methods:{
		getLastMoney(){
			if(this.orderMoney - this.couponPrice - this.activitesPrice<=0){
				return Number(this.hotelObj.deposit).toFixed(2);
			}else{
				let num = this.orderMoney - this.couponPrice - this.activitesPrice+Number(this.hotelObj.deposit)
				return num;
			}
		},
		preSecondOrder(){
			if(this.occupantsIndex.length<1){
				uni.showToast({
					icon:"none",
					title:"请选择入住人"
				})
				return;
			}
			uni.showLoading({
				mask:true,
			});
			preSecondOrder({
				orderNo:this.orderNo,
				...this.hotelObj,
				roomTypeId:this.hotelObj.roomId,
				payType: Number(this.payTypeIndex)+1,
				endTime:this.time[1],
				hours:this.hours,
				orderType:this.type+1,
				orderSource:1,
				shareOrder:this.switchA?1:0,
				occupants:this.occupantsIndex.map((item,index)=>{
					return this.occupants[Number(item)].id
				})
			}).then(res=>{
				if(res.code==200&&res.data.prePayInfo){
					let obj = res.data.prePayInfo;
					uni.requestPayment({
						provider:"wxpay",
						timeStamp:obj.timeStamp,
						nonceStr:obj.nonceStr,
						'package':obj.packageValue,
						paySign:obj.paySign,
						signType:obj.signType,
						success(e) {
							if(e.errMsg=='requestPayment:ok'){
								uni.showLoading({
									title:"正在下单请稍等"
								})
								checkPayStatus({
									orderInfoId:res.data.orderInfo.id,
									outTradeNo:res.data.outTradeNo
								}).then(payRes=>{
									uni.hideLoading();
									if(payRes.code==200){
										uni.switchTab({
											url:"/pages/tabbar/cool/cool"
										})
									}else{
										uni.redirectTo({
											url:"/pages/mine/orderList/orderList"
										})
									}
								})
							}
						},
						fail(err) {
							uni.hideLoading();
							if(err.errMsg=='requestPayment:fail cancel'){
								uni.showToast({
									title:"取消支付，请10分钟内到订单列表内支付",
									icon:"none",
									mask:true,
									duration:2000
								})
								setTimeout(()=>{
									uni.redirectTo({
										url:"/pages/mine/orderList/orderList"
									})
								},2000)
							}
							console.log(err);
						}
					})
				}else if(res.code==200){
					checkPayStatus({
						orderInfoId:res.data.orderInfo.id,
						outTradeNo:res.data.outTradeNo
					}).then(payRes=>{
						uni.hideLoading();
						if(payRes.code==200){
							uni.switchTab({
								url:"/pages/tabbar/cool/cool"
							})
						}else{
							uni.redirectTo({
								url:"/pages/mine/orderList/orderList"
							})
						}
					})
				}
				
			})
		},
		sliderChange(event){
			if(event){
				this.hours = event.detail.value;
			}
			clacSecondPrice({
				roomTypeId:this.hotelObj.roomId,
				 hours:this.hours
			}).then(res=>{
				if(res.code==200){
					this.secondPrice = res.data.toFixed(2);
				}
			})
		},
		SwitchA(e) {
			this.switchA = e.detail.value
		},
		submitOrder(){
			if(this.occupantsIndex.length<1){
				uni.showToast({
					icon:"none",
					title:"请选择入住人"
				})
				return;
			}
			uni.showLoading({
				mask:true,
			});
			orderSave({
				orderNo:this.orderNo,
				...this.hotelObj,
				roomTypeId:this.hotelObj.roomId,
				payType: Number(this.payTypeIndex)+1,
				couponId:this.coupon.id,
				activites:this.activites.id,
				beginTime:this.time[0],
				endTime:this.time[1],
				orderType:this.type+1,
				orderSource:1,
				shareOrder:this.switchA?1:0,
				occupants:this.occupantsIndex.map((item,index)=>{
					return this.occupants[Number(item)].id
				})
			}).then(res=>{
				if(res.code==200&&res.data.prePayInfo){
					let obj = res.data.prePayInfo;
					uni.requestPayment({
						provider:"wxpay",
						timeStamp:obj.timeStamp,
						nonceStr:obj.nonceStr,
						'package':obj.packageValue,
						paySign:obj.paySign,
						signType:obj.signType,
						success(e) {
							if(e.errMsg=='requestPayment:ok'){
								uni.showLoading({
									title:"正在下单请稍等"
								})
								checkPayStatus({
									orderInfoId:res.data.orderInfo.id,
									outTradeNo:res.data.outTradeNo
								}).then(payRes=>{
									uni.hideLoading();
									if(payRes.code==200){
										uni.switchTab({
											url:"/pages/tabbar/cool/cool"
										})
									}else{
										uni.redirectTo({
											url:"/pages/mine/orderList/orderList"
										})
									}
								})
							}
						},
						fail(err) {
							uni.hideLoading();
							if(err.errMsg=='requestPayment:fail cancel'){
								uni.showToast({
									title:"取消支付，请10分钟内到订单列表内支付",
									icon:"none",
									mask:true,
									duration:2000
								})
								setTimeout(()=>{
									uni.redirectTo({
										url:"/pages/mine/orderList/orderList"
									})
								},2000)
							}
							console.log(err);
						}
					})
				}else if(res.code==200){
					checkPayStatus({
						orderInfoId:res.data.orderInfo.id,
						outTradeNo:res.data.outTradeNo
					}).then(payRes=>{
						uni.hideLoading();
						if(payRes.code==200){
							uni.switchTab({
								url:"/pages/tabbar/cool/cool"
							})
						}else{
							uni.redirectTo({
								url:"/pages/mine/orderList/orderList"
							})
						}
					})
				}
				
			})
		},
		delOccupantsIndex(num){
			let index = this.occupantsIndex.indexOf(num.toString());
			this.occupantsIndex.splice(index,1);
		},
		couponMoney(){
			if(this.coupon.id){
				if(this.coupon.couponType==1){
					return " -"+this.coupon.couponDiscount
				}else{
					return " -"+Number((1-this.coupon.couponDiscount)*this.orderMoney).toFixed(2);
				}
			}else{
				return "";
			}
		},
		activitesMoney(){
			if(this.activites.id){
				if(this.activites.discountType==2){
					return " -"+this.activites.discountNum;
				}else{
					return " -"+Number((1-this.activites.discountNum)*this.orderMoney).toFixed(2);
				}
			}else {
				return ""
			}
		},
		changeMoney(){
			let couponPrice;
			if(this.coupon.id){
				if(this.coupon.couponType==1){
					couponPrice = Number(this.coupon.couponDiscount);
				}else{
					couponPrice = Number((1-this.coupon.couponDiscount)*this.orderMoney).toFixed(2);
				}
				this.couponPrice = couponPrice;
			}
			let activitesPrice;
			if(this.activites.id){
				if(this.activites.discountType==2){
					activitesPrice = Number(this.activites.discountNum);
				}else{
					activitesPrice = Number((1-this.activites.discountNum)*this.orderMoney).toFixed(2);
				}
				this.activitesPrice = activitesPrice;
			}
		},
		getActivites(){
			getActivites({
				hotelId:this.hotelObj.hotelId,
				roomTypeId:this.hotelObj.roomId,
				areaCode:this.chooseCity.areaCode,
				startDate:this.time[0],
				endDate:this.time[1]
			}).then(res=>{
				this.activitesList = res.data;
			})
		},
		getAbleCoupon(){
			ableCoupon({
				hotelId:this.hotelObj.hotelId,
				roomTypeId:this.hotelObj.roomId,
				areaCode:this.chooseCity.areaCode,
				startDate:this.time[0],
				endDate:this.time[1]
			}).then(res=>{
				this.couponNum = res.data.length;
				this.couponList = res.data;
			})
		},
		getOrderPrice(){
			getOrderPrice({
				startDate:this.time[0],
				endDate:this.time[1],
				roomTypeId:this.hotelObj.roomId
			}).then(res=>{
				let orderMoney = 0;
				for(let i in res.data){
					orderMoney += res.data[i];
				}
				console.log(orderMoney);
				this.orderMoney = orderMoney;
			})
		},
		selectCoupon(){
			let obj = {
				hotelId:this.hotelObj.hotelId,
				roomTypeId:this.hotelObj.roomId,
				areaCode:this.chooseCity.areaCode,
				startDate:this.time[0],
				endDate:this.time[0]
			}
			uni.navigateTo({
				url:"/pages/index/selectCoupon/selectCoupon"+this.$u.queryParams(obj)
			})
		},
		selectActivites(){
			let obj = {
				hotelId:this.hotelObj.hotelId,
				roomTypeId:this.hotelObj.roomId,
				areaCode:this.chooseCity.areaCode,
				startDate:this.time[0],
				endDate:this.time[0]
			}
			uni.navigateTo({
				url:"/pages/index/activites/activites"+this.$u.queryParams(obj)
			})
		},
		getXh(phone){
			if(!phone){
				return ""
			}
			return phone.substr(0, 3) + '****' + phone.substr(7);
		},
		initTime(){
			if(!this.type){
				this.num = parseInt(((new Date(this.time[1]).getTime() - new Date(this.time[0]).getTime())/(1000*60*60*24)));
			}
		},
		getGuests(){
			guestsList().then(res=>{
				this.occupants=res.data;
			})
		},
		changeTime(e){
			let time = [];
			if(this.type){
				time[0]=e.result;
			}else{
				if(e.startDate==e.endDate){
					return;
				}
				time[0]=e.startDate;
				time[1]=e.endDate;
				this.num = parseInt(((new Date(e.endDate).getTime() - new Date(e.startDate).getTime())/(1000*60*60*24)));
			}
			this.$store.dispatch('SetTime',time);
			if(!this.type){
				this.getOrderPrice();
			}
		},
		payTypeChange(e){
			this.payTypeIndex=e.detail.value
		},
		occupantsChange(e){
			this.occupantsIndex=e.detail.value;
		},
		newPeople(people){
			if(people){
				uni.navigateTo({
					url:"../newPeople/newPeople"+this.$u.queryParams(people)
				})
			}else{
				uni.navigateTo({
					url:"../newPeople/newPeople"
				})
			}
			
		}
	}
}